#include <curl/curl.h>

// int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
// {
//        int res;
//        CURL* curl = curl_easy_init();
//        if (NULL == curl)
//        {
//               return CURLE_FAILED_INIT;
//        }
//        if (m_bDebug)
//        {
//               curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
//               curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
//        }
//        curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
//        curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
//        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
//        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
//        /* enable TCP keep-alive for this transfer */
//        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
//        /* keep-alive idle time to 120 seconds */
//        curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
//        /* interval time between keep-alive probes: 60 seconds */
//        curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 20L);
//        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
//        /**
//        * 当多个线程都使用超时处理的时候，同时主线程中有sleep或是wait等操作。
//        * 如果不设置这个选项，libcurl将会发信号打断这个wait从而导致程序退出。
//        */
//        //curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
//        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20);
//        res = curl_easy_perform(curl);
//        if (res != 0)
//        {
//               //FIRE_ERROR("  Get error %d", res);
//        }
//     //CurlMutiTreadMutex::GetInstance()->muti_curl_easy_cleanup(curl);
//        curl_easy_cleanup(curl);
//        return res;
// }

CURL* GetCurl();
CURL* CreateCurl();
void PutCurl(CURL* curl);
QVector<CURL*> m_VectCurl;
QMutex m_mutex;

CURL* RestClientPool::GetCurl()
{
    CURL* curl = NULL;
    m_mutex.lock();
    if (m_VectCurl.size()>0)
    {
        curl = m_VectCurl.front();
        m_VectCurl.pop_front();
    }
    m_mutex.unlock();
    if(curl==NULL)
    {
        curl = CreateCurl();
    }
    return curl;
}
 
CURL* RestClientPool::CreateCurl()
{
    CURL* curl = curl_easy_init();
    if (NULL == curl)
    {
        return NULL;
    }
    if (m_bDebug)
    {
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
    }
    //curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
    /* enable TCP keep-alive for this transfer */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
    /* keep-alive idle time to 120 seconds */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 300L);
    /* interval time between keep-alive probes: 60 seconds */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 20L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
    /**
    * 当多个线程都使用超时处理的时候，同时主线程中有sleep或是wait等操作。
    * 如果不设置这个选项，libcurl将会发信号打断这个wait从而导致程序退出。
    */
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20);
    return curl;
}
 
void RestClientPool::PutCurl(CURL* curl)
{
    m_mutex.lock();
    m_VectCurl.push_back(curl);
    m_mutex.unlock();
}
int RestClientPool::Get(const std::string & strUrl, std::string & strResponse)
{
    int res;
    //CURL* curl = CurlMutiTreadMutex::GetInstance()->muti_curl_easy_init();
    CURL* curl = GetCurl();
    if (NULL == curl)
    {
        return CURLE_FAILED_INIT;
    }
   
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
    res = curl_easy_perform(curl);
    if (res != 0)
    {
        printf("req error %d",res);
    }
    PutCurl(curl);
    return res;
}